using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace C4F_Ocean
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;

        private Camera camera;

        private Ocean ocean;

        private SkyBox skyBox;
        private Effect skyboxEffect;
        private TextureCube skyTex;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // Set the global graphics device
            Global.Graphics = graphics.GraphicsDevice;

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // create our three game objects
            skyBox = new SkyBox();
            ocean = new Ocean();
            camera = new Camera();

            // let the ocean load its stuff
            ocean.Load(Content);

            // load some skybox content
            skyboxEffect = Content.Load<Effect>("SkyShader");
            skyTex = Content.Load<TextureCube>("Sky");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            // update the camera based on user input
            camera.Update(gameTime);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            // create a standard projection matrix
            Matrix proj = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, Global.ScreenWidth / (float)Global.ScreenHeight, 1.0f, 1000.0f);

            // the sky is never culled and should not write to the depth buffer
            Global.Graphics.RenderState.CullMode = CullMode.None;
            Global.Graphics.RenderState.DepthBufferEnable = false;

            skyboxEffect.Begin();
            skyboxEffect.CurrentTechnique.Passes[0].Begin();

            // set the sky shader variables
            skyboxEffect.Parameters["View"].SetValue(camera.GetViewMatrix());
            skyboxEffect.Parameters["Projection"].SetValue(proj);
            skyboxEffect.Parameters["cubeTex"].SetValue(skyTex);
            skyboxEffect.CommitChanges();

            // and draw
            skyBox.Draw();

            skyboxEffect.CurrentTechnique.Passes[0].End();
            skyboxEffect.End();

            // turn depth buffering back on for the ocean
            Global.Graphics.RenderState.DepthBufferEnable = true;
            Global.Graphics.Clear(ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);

            // and allow the ocean to draw itself
            ocean.Draw(gameTime, camera, skyTex, proj);

            base.Draw(gameTime);
        }
    }
}
